<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Session;
use think\Cache;
use think\Controller;
use think\captcha\Captcha;
class Console extends  Common
{
	//广告位置列表
	public function get_info(){
		//赛程  等待比赛
		$one_num=Db::name('match')->where(array('type'=>1))->count();
		//比赛中
		$two_num=Db::name('match')->where(array('type'=>2))->count();
		//比赛结束
		$three_num=Db::name('match')->where(array('type'=>3))->count();
		//新闻待审核
		$four_num=Db::name('news')->where(array('status'=>2))->count();
		//视频待审核
		$five_num=Db::name('video')->where(array('status'=>2))->count();
		$result['code'] = 0;
		$result['msg'] = 'ok';
		$result['one_num'] = $one_num;
		$result['two_num'] = $two_num;
		$result['three_num'] = $three_num;
		$result['four_num'] = $four_num;
		$result['five_num'] = $five_num;
		$data = json_encode($result);
		$data=str_replace('null','""',$data);
		echo $data;
	}
	public function get_register_list(){
		$result=array();
		$result['code'] = 0;
		$result['msg'] = 'ok';
	
		/*$time_list=array();
		for($i=0;$i<=12;$i++){
			$time_list[$i]=($i*2).':00';
		}
		$result['time_list']=$time_list;
		for($i=0;$i<12;$i++){
		   $time_start=strtotime(date('Y-m-d',time()).' '.($i*2).':00:00');
		   $time_end=strtotime(date('Y-m-d',time()).' '.($i*2+2).':59:59');
			$num_list[$i]=Db::name('user')->where('createtime','between',"$time_start,$time_end")->count();
		}
		$result['num_list']=$num_list;*/
		//新闻一周新增
		$newsdate_list=array();
		$start_date=strtotime(date('Y-m-d',time()))-6*86400;
		for($i=0;$i<7;$i++){
			$newsdate_list[$i]=date('m-d',$start_date+$i*86400);
		}
		$result['newsdate_list']=$newsdate_list;
		for($i=0;$i<7;$i++){
			$date_start=strtotime(date('Y-m-d',$start_date+($i*86400)).' 00:00:00');
			$date_end=strtotime(date('Y-m-d ',$start_date+($i*86400)).' 23:59:59');
			$datenum_list[$i]=Db::name('news')->where('createtime','between',"$date_start,$date_end")->count();
		}
		$result['newsnum_list']=$datenum_list;
		//视频一周新增
		$videodate_list=array();
		$start_date=strtotime(date('Y-m-d',time()))-6*86400;
		for($i=0;$i<7;$i++){
			$videodate_list[$i]=date('m-d',$start_date+$i*86400);
		}
		$result['videodate_list']=$videodate_list;
		for($i=0;$i<7;$i++){
			$date_start=strtotime(date('Y-m-d',$start_date+($i*86400)).' 00:00:00');
			$date_end=strtotime(date('Y-m-d ',$start_date+($i*86400)).' 23:59:59');
			$videonum_list[$i]=Db::name('video')->where('createtime','between',"$date_start,$date_end")->count();
		}
		$result['videonum_list']=$videonum_list;
		//会员一周新增
		$userdate_list=array();
		$start_date=strtotime(date('Y-m-d',time()))-6*86400;
		for($i=0;$i<7;$i++){
			$userdate_list[$i]=date('m-d',$start_date+$i*86400);
		}
	    $result['date_list']=$userdate_list;
		for($i=0;$i<7;$i++){
			$date_start=strtotime(date('Y-m-d',$start_date+($i*86400)).' 00:00:00');
			$date_end=strtotime(date('Y-m-d ',$start_date+($i*86400)).' 23:59:59');
			$datenum_list[$i]=Db::name('user')->where('createtime','between',"$date_start,$date_end")->count();
		}
		$result['datenum_list']=$datenum_list;
		$result=json_encode($result);
		echo $result;
	}
	public function system_info(){
		$info = new \app\admin\model\Systeminfo();
		$cpu = $info->getCpuUsage();
		$memory = $info->getMemoryUsage();


		$result=array();
		$result['code'] = 0;
		$result['msg'] = 'ok';
		$result['cpu']=10;
		$result['memory']=10;
		echo json_encode($result);
	}
	//今日热闻
	public function hot_news(){
		$p=input('page')?input('page'):1;
		$pagesize=input('limit')?input('limit'):10;
		$start=($p-1)*$pagesize;
		$where=array();
	
		$where['status']=array('eq',1);
		
		$start_time=strtotime(date('Y-m-d',time()).' 00:00:00');
		$end_time=strtotime(date('Y-m-d',time()).' 23:59:59');
		$where['createtime']=array('between',[$start_time,$end_time]);

		$count=Db::name('news')->field('*')->where($where)->count();
		if($count>0){
			/*$list = Db::table('qm_news')
				->alias('n')
				->field('n.*,c.name as cate_name')
				->join('qm_news_cate c','n.cate_id = c.id')
				->select();*/
			$pagenum=ceil($count/$pagesize);
			if($p<=$pagenum) {
				$list=Db::name('news')->field('id,title,author,hits')->limit($start, $pagesize)->where($where)->order('hits desc')->select();
				$result['code'] = 0;
				$result['msg'] = 'ok';
				$result['count'] = $count;

				$result['data'] = $list;
				$data = json_encode($result);
				$data=str_replace('null','""',$data);
			}else{
				$result['code'] = 1;
				$result['msg'] = '超出最大页数';
				$data = json_encode($result);
				$data=str_replace('null','""',$data);
			}
		}else{
			$data='{"errcode":"1","msg":"没有数据"}';
		}
		echo $data;
	}
	//今日热评
	public function hot_comment(){
		$p=input('page')?input('page'):1;
		$pagesize=input('limit')?input('limit'):10;
		$start=($p-1)*$pagesize;
		$where=array();
	
		$where['status']=array('eq',1);
		
		$start_time=strtotime(date('Y-m-d',time()).' 00:00:00');
		$end_time=strtotime(date('Y-m-d',time()).' 23:59:59');
		$where['createtime']=array('between',[$start_time,$end_time]);

		$count=Db::name('comment')->field('*')->where($where)->count();
		if($count>0){
			/*$list = Db::table('qm_news')
				->alias('n')
				->field('n.*,c.name as cate_name')
				->join('qm_news_cate c','n.cate_id = c.id')
				->select();*/
			$pagenum=ceil($count/$pagesize);
			if($p<=$pagenum) {
				$list=Db::name('comment')->field('id,content,nickname,type,type_id,zan_num')->limit($start, $pagesize)->where($where)->order('zan_num desc')->select();
				foreach ($list as $k=>$v){
					switch ($list[$k]['type']){
						case 1:
							$list[$k]['type_name']='新闻';
							$news_re=Db::name('news')->field('title')->where(array('id'=>$list[$k]['type_id']))->find();
							$list[$k]['type_title']=$news_re['title'];
							break;
						case 2:
							$list[$k]['type_name']='视频';
							$video_re=Db::name('video')->field('title')->where(array('id'=>$list[$k]['type_id']))->find();
							$list[$k]['type_title']=$video_re['title'];
							break;
						case 3:
							$list[$k]['type_name']='赛程';
							$match_re=Db::name('match')->field('name')->where(array('id'=>$list[$k]['type_id']))->find();
							$list[$k]['type_title']=$match_re['name'];
							break;
					}

				}
				$result['code'] = 0;
				$result['msg'] = 'ok';
				$result['count'] = $count;

				$result['data'] = $list;
				$data = json_encode($result);
				$data=str_replace('null','""',$data);
			}else{
				$result['code'] = 1;
				$result['msg'] = '超出最大页数';
				$data = json_encode($result);
				$data=str_replace('null','""',$data);
			}
		}else{
			$data='{"errcode":"1","msg":"没有数据"}';
		}
		echo $data;
	}
	//会员
	public function user_info(){
		//会员注册  周
		$week_start= mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"));
		$week_end= mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"));
		$one_num=Db::name('user')->where('createtime','between',[$week_start,$week_end])->count();
		//会员注册 月
		$month_start=mktime(0, 0 , 0,date("m"),1,date("Y"));
		$month_end=mktime(23,59,59,date("m"),date("t"),date("Y"));
		$two_num=Db::name('user')->where('createtime','between',[$month_start,$month_end])->count();
		//会员注册  年
		$year_start=strtotime(date('Y').'-01-01 00:00:00');
		$year_end=strtotime(date('Y').'-12-31 23:59:59');
		$three_num=Db::name('user')->where('createtime','between',[$year_start,$year_end])->count();
		//会员活跃  月
		$four_num=Db::name('user')->where('login_time','between',[$month_start,$month_end])->count();
		//会员总数
		$count=Db::name('user')->count();
		$one_percent=round($one_num/$count,2)*100;
		$two_percent=round($two_num/$count,2)*100;
		$three_percent=round($three_num/$count,2)*100;
		$four_percent=round($four_num/$count,2)*100;

		$result['code'] = 0;
		$result['msg'] = 'ok';
		$result['one_num'] = $one_num;
		$result['two_num'] = $two_num;
		$result['three_num'] = $three_num;
		$result['four_num'] = $four_num;
		$result['count'] = $count;
		$result['one_percent'] = $one_percent.'%';
		$result['two_percent'] = $two_percent.'%';
		$result['three_percent'] = $three_percent.'%';
		$result['four_percent'] = $four_percent.'%';
		$data = json_encode($result);
		$data=str_replace('null','""',$data);
		echo $data;
	}
	//会员注册量、活跃量
	public function user_list(){
		$arr=[1,2,3,4,5,6,7,8,9,10,11,12];
		$register_list=array();
		foreach ($arr as $k=>$v){
			$start=strtotime(date('Y').'-'.$arr[$k].'-01 00:00:00');
			$end=strtotime(date('Y').'-'.$arr[$k].'-'. date("t",strtotime(date('Y').'-'.$arr[$k].'-01')).' 23:59:59');
			$count=Db::name('user')->where('createtime','between',[$start,$end])->count();
			$register_list[$k]=$count?$count:0;
		}
	
		$active_list=array();
		foreach ($arr as $k=>$v){
			$start=strtotime(date('Y').'-'.$arr[$k].'-01 00:00:00');
			$end=strtotime(date('Y').'-'.$arr[$k].'-'. date("t",strtotime(date('Y').'-'.$arr[$k].'-01')).' 23:59:59');
			$count=Db::name('user')->where('login_time','between',[$start,$end])->count();
			$active_list[]=$count?$count:0;
		}
		$result['code']='0';
		$result['msg']='ok';
		$result['register_list']=$register_list;
		$result['active_list']=$active_list;
		echo json_encode($result);
	}
	//会员注册、活跃较上月增长
	public function increase(){
		//上月会员注册  月
		$lastmonth_start=mktime(0, 0 , 0,date("m")-1,1,date("Y"));
		$lastmonth_end=mktime(23,59,59,date("m") ,0,date("Y"));
		$lastone_num=Db::name('user')->where('createtime','between',[$lastmonth_start,$lastmonth_end])->count();
		//上月会员活跃  月
		$lasttwo_num=Db::name('user')->where('login_time','between',[$lastmonth_start,$lastmonth_end])->count();
		//上月新闻新增  月
		$lastthree_num=Db::name('news')->where('createtime','between',[$lastmonth_start,$lastmonth_end])->count();

		//本月会员注册 月
		$month_start=mktime(0, 0 , 0,date("m"),1,date("Y"));
		$month_end=mktime(23,59,59,date("m"),date("t"),date("Y"));
		$one_num=Db::name('user')->where('createtime','between',[$month_start,$month_end])->count();
		//本月会员活跃  月
		$two_num=Db::name('user')->where('login_time','between',[$month_start,$month_end])->count();
		//本月新闻新增  月
		 $three_num=Db::name('news')->where('createtime','between',[$month_start,$month_end])->count();
		if($lastone_num>0){
			if($one_num){
				$one_percentage=round(($one_num-$lastone_num)/$lastone_num,2)*100;
			}else{
				$one_percentage='-100';
			}
		}else{
			$one_percentage='100';
		}

		if($lasttwo_num>0){
			if($two_num>0){
				$two_percentage=round(($two_num-$lasttwo_num)/$lasttwo_num,2)*100;
			}else{
				$two_percentage='-100';
			}
		}else{
			$two_percentage='100';
		}
		if($lastthree_num>0){
			if($three_num){
				$three_percentage=round(($three_num-$lastthree_num)/$lastthree_num,2)*100;
			}else{
				$three_percentage='-100';
			}
		}else{
			$three_percentage='100';
		}

		$result['code']='0';
		$result['msg']='ok';
		$result['one_percentage']=$one_percentage.'%';
		$result['two_percentage']=$two_percentage.'%';
		$result['three_percentage']=$three_percentage.'%';
		echo json_encode($result);
	}
	//会员反馈
	public function user_feedback(){
		$list=Db::name('feedback')->field('id,content,contact,createtime')->limit(7)->order('createtime desc')->select();
		if(empty($list)){
			$list=array();
		}else{
			foreach ($list as $k=>$v){
				$list[$k]['month']=date('m',$list[$k]['createtime']);
				$list[$k]['day']=date('d',$list[$k]['createtime']);
				$list[$k]['time']=date('H:i',$list[$k]['createtime']);
			}
		}
		$result['code']='0';
		$result['msg']='ok';
		$result['data']=$list;
		echo json_encode($result);
	}
	//本周活跃用户
	public function user_active(){
		$week_start= mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"));
		$week_end= mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"));
		$list=Db::name('user')->field('id,name,login_time,status')->where('login_time','between',[$week_start,$week_end])->limit(7)->order('login_time desc')->select();
		if(empty($list)){
			$list=array();
		}else{
			foreach ($list as $k=>$v){
				$list[$k]['time']=date('m-d H:i',$list[$k]['login_time']);
				if($list[$k]['status']==1){
					$list[$k]['status_name']='正常';
				}elseif($list[$k]['status']==2){
					$list[$k]['status_name']='冻结';
				}else{
					$list[$k]['status_name']='正常';
				}
			}
		}
		$result['code']='0';
		$result['msg']='ok';
		$result['data']=$list;
		echo json_encode($result);
	}
	//赛程进展
	public function match_list(){
		$list=Db::name('match')->field('id,name,type')->where('status=1')->limit(7)->order('createtime desc')->select();
		if(empty($list)){
			$list=array();
		}else{
			foreach ($list as $k=>$v){
				if($list[$k]['type']==1){
					$list[$k]['type_name']='等待比赛';
				}elseif($list[$k]['type']==2){
					$list[$k]['type_name']='比赛中';
				}elseif($list[$k]['type']==3){
					$list[$k]['type_name']='已结束';
				}else{
					$list[$k]['type_name']='等待比赛';
				}
			}
		}
		$result['code']='0';
		$result['msg']='ok';
		$result['data']=$list;
		echo json_encode($result);
	}
	//会员全国分布
	public function user_nationwide(){
		$arr[0]['name']='西藏';$arr[0]['value']=0;
		$arr[1]['name']='青海';$arr[1]['value']=0;
		$arr[2]['name']='宁夏';$arr[2]['value']=0;
		$arr[3]['name']='海南';$arr[3]['value']=0;
		$arr[4]['name']='甘肃';$arr[4]['value']=0;
		$arr[5]['name']='贵州';$arr[5]['value']=0;
		$arr[6]['name']='新疆';$arr[6]['value']=0;
		$arr[7]['name']='云南';$arr[7]['value']=0;
		$arr[8]['name']='重庆';$arr[8]['value']=0;
		$arr[9]['name']='吉林';$arr[9]['value']=0;
		$arr[10]['name']='山西';$arr[10]['value']=0;
		$arr[11]['name']='天津';$arr[11]['value']=0;
		$arr[12]['name']='江西';$arr[12]['value']=0;
		$arr[13]['name']='广西';$arr[13]['value']=0;
		$arr[14]['name']='陕西';$arr[14]['value']=0;
		$arr[15]['name']='黑龙江';$arr[15]['value']=0;
		$arr[16]['name']='内蒙古';$arr[16]['value']=0;
		$arr[17]['name']='安徽';$arr[17]['value']=0;
		$arr[18]['name']='北京';$arr[18]['value']=0;
		$arr[19]['name']='福建';$arr[19]['value']=0;
		$arr[20]['name']='上海';$arr[20]['value']=0;
		$arr[21]['name']='湖北';$arr[21]['value']=0;
		$arr[22]['name']='湖南';$arr[22]['value']=0;
		$arr[23]['name']='四川';$arr[23]['value']=0;
		$arr[24]['name']='辽宁';$arr[24]['value']=0;
		$arr[25]['name']='河北';$arr[25]['value']=0;
		$arr[26]['name']='河南';$arr[26]['value']=0;
		$arr[27]['name']='浙江';$arr[27]['value']=0;
		$arr[28]['name']='山东';$arr[28]['value']=0;
		$arr[29]['name']='江苏';$arr[29]['value']=0;
		$arr[30]['name']='广东';$arr[30]['value']=0;
		foreach ($arr as $k=>$v){
			$count=Db::name('user')->where(array('province'=>$arr[$k]['name']))->count();
			$arr[$k]['value']=$count;
		}
		$result['code']='0';
		$result['msg']='ok';
		$result['list']=$arr;
		$Index=new \app\admin\model\Index();
		$new_arr = $Index->my_sort($arr,'value',SORT_DESC,SORT_STRING);
		$new_arr=array_splice($new_arr,0,6);
		$result['new_arr']=$new_arr;
		echo json_encode($result);
	}

}
